var registerCrtl = new XFApp.client();
XFApp.client.prototype.register = {
		successCode : 10000,
		urlItems : {			
			registerURL: currentHost+"/register/submit",	
			checkUserNameURL: currentHost+"/register/checkUserName",
			checkCompanyURL: currentHost+"/register/checkCompany",
		},
		
		formValidate : function(){
			$("#regForm").validate({
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());					
					else
						error.insertAfter(element); 
				},
		        rules: {
		        	tel: "required",		        	
		        	pwd:  {
		        		 	required:true,
		        		 	minlength:6,
			        		maxlength:20
		        		   },
		            repwd: {
		        		    required:true,
		        		    equalTo: "#pwd"
		        	       },
		        	email: {
		        		    required:true,
		        		  	email: true
		        		   },		        	                   			   
		        	agr: "required"
				  },
				 messages: {
					 tel: "请填写联系电话",
			         pwd: {
			        	       required:"请填写密码",
		        	           minlength: $.validator.format("请至少输入{0}个字符"),
		        	           maxlength: $.validator.format("请最多输入{0}个字符"),
			         		   },
			         repwd:{
			        	 		required:"请再次输入密码",
			        	 		equalTo:"两次输入的密码不一致"
			         			},
			         email:     {
			        	 		required:"请填写电子邮件",
			        	 		email:"电子邮件格式不正确"
			         			},
			       
			         agr: "请同意网站服务协议"       	
				  }
		    });
			$("#regForm").validate();
		},
		
		event : function (){			
			var _this = this;
			// 注册        
	        $('#registerBtn').click(function(){	           
	        	
	        	if(!$("#username").val()){
					$("#username-error").html('请填写用户名');
				}
	        	
	        	if(!$("#company").val()){
					$("#company-error").html('请填写公司名称');
				}        	
	        	
	        	if($("#regForm").validate().form()){
	        		if(!$("#username-error").text() 
	        			&& !$("#company-error").text()){
	        			registerCrtl.register.submit();
	        		}
	        	}
	        	
	        })	
	        
	        //表单验证
			_this.formValidate();
	        
	        $("#username").on("blur",function(){
	        	
	        	if(!$("#username").val()){
					$("#username-error").html('请填写用户名');
				}else{
					$("#username-error").html('');
				}	        	
	        	if(!$("#username-error").text()){
	        		_this.checkUserName();
	        	}
			});
	        
	        $("#company").on("blur",function(){
	        	if(!$("#company").val()){
					$("#company-error").html('请填写公司名称');
				}else{
					$("#company-error").html('');
				}
	        	if(!$("#company-error").text()){
	        		_this.checkCompany();
	        	}				
			});
	        
		},	
		
		
		checkUserName : function (){
			var _this = this;
			var params = {
				username : $("#username").val()
		    }
			
			$.ajax({
		    	url :_this.urlItems.checkUserNameURL,
		    	data : params,
		    	dataType : "json",
		    	type : 'post',
		    	async:false,
		    	success : function(data) {				
		    		if(data.code==_this.successCode){ 
		    			$("#username-error").html("");
		    		}else{
		    			$("#username-error").html(data.msg);
		    		}
		    	}
		    });			
			
		},
		
		
		checkCompany:function(){
			var _this = this;
			var params = {
				company : $("#company").val()
			}
				
			$.ajax({
			    url :_this.urlItems.checkCompanyURL,
			    data : params,
			    dataType : "json",
			    type : 'post',
			    async:false,
			    success : function(data) {				
			    	if(data.code==_this.successCode){ 
			    		$("#company-error").html("");
			    	}else{
			    		$("#company-error").html(data.msg);
			    	}
			    }
			});	
			
		},	
		
		//提交
		submit : function (){
			var _this = this;
			var userEx = {
				loginName : $("#username").val(),
				userName : $("#company").val(),
				password : $("#pwd").val(),
				cellphone : $("#tel").val(),
				email : $("#email").val(),
	        }
		    	
		    $.ajax({
		    	url :_this.urlItems.registerURL,
		    	data : userEx,
		    	dataType : "json",
		    	type : 'post',
		    	success : function(data) {				
		    		if(data.code==_this.successCode){ 
		    			window.webDialogUtil.alert("提示",data.msg);		    			
		    			$("#regForm")[0].reset();		    			
		    		}else{
		    			window.webDialogUtil.alert("提示",data.msg);
		    		}
		    	}
		    });
		}
},


$(function(){	
	
	registerCrtl.register.event();
	
});